package si.yun.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import si.yun.dao.PersonnelDao;
import si.yun.entity.SyClosure;
import si.yun.entity.SyClosureLog;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: SiYunClass
 * @description: 创建定时器解封用户账号
 * @author: 崔晓晨
 * @create: 2021-08-20 14:42
 **/
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class DynamicScheduleTask implements SchedulingConfigurer {
    //每天的凌晨0点执行
    private String cron = "0 0 0 * * ?";
    //private String cron = "0/6 * * * * ?";
    @Autowired
    private PersonnelDao dao;
    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> System.out.println("每日执行解封任务: " + LocalDateTime.now().toLocalTime()),
                //2.设置执行周期(Trigger)
                triggerContext -> {
                    //3 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                        // Omitted Code ..
                    }
                    String endDate = String.valueOf(LocalDateTime.now().toLocalDate());
                    List<SyClosure> closures = dao.selCloUserInfo(endDate);
                    for(SyClosure clo:closures){
                        Integer type = clo.getType();
                        if(type==2){
                            String rea = clo.getReason();
                            String reason = rea+",自动解封（已解封）";
                            clo.setReason(reason);
                            Integer status = 3;
                            String tableName = "sy_before_user";
                            updStatus(clo,status,tableName);
                        }else if(type==3){
                            String rea = clo.getReason();
                            String reason = rea+",自动解封（已解封）";
                            clo.setReason(reason);
                            Integer status = 3;
                            String tableName = "sy_user";
                            updStatus(clo,status,tableName);
                        }
                    }
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }
    /**
     * @Description  解封用户
     * @return si.yun.utils.JsonData
     * @Date 2021/9/3 20:07
     **/
    private JsonData updStatus(SyClosure syClosure, Integer status, String tableName) {
        Integer userId = syClosure.getUserId();
        Integer upd = dao.updStatus(tableName, userId, status);
        SyClosureLog syClosureLog = new SyClosureLog();
        syClosureLog.setOperationId(0);
        syClosureLog.setUserId(userId);
        syClosureLog.setType(syClosure.getType());
        syClosureLog.setRemark("自动解封");
        dao.updClosure(syClosure);
        dao.addClosureLog(syClosureLog);
        return upd != 0 ? JsonData.buildSuccess(upd) : JsonData.buildError("服务器异常！请耐心等待修复！");
    }

}
